package com.hutao.manage.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

public class FileUtil {

	public static boolean createOrEditFile(String data,String fileDir,String fileName) throws IOException{
		File destFile = new File(fileDir,fileName);
		if(!destFile.exists()){
			File destDir = new File(fileDir);
			if(!destDir.exists()){
				destDir.mkdirs();
			}
			destFile.createNewFile();
		}
		
		FileOutputStream fileOutputStream = null;
		OutputStreamWriter outputWriter = null;
		
		try {
			  fileOutputStream=new FileOutputStream(destFile);  
	          outputWriter=new OutputStreamWriter(fileOutputStream,"UTF-8");  
	          outputWriter.write(data);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(outputWriter);
			IOUtils.closeQuietly(fileOutputStream);
		}
       
		return true;
	}
	
	public static boolean createFile(String data,String fileDir,String fileName) throws IOException{
		File destFile = new File(fileDir,fileName);
		if(!destFile.exists()){
			File destDir = new File(fileDir);
			if(!destDir.exists()){
				destDir.mkdirs();
			}
			destFile.createNewFile();
		} else {
			return false;
		}
		
		FileOutputStream fileOutputStream = null;
		OutputStreamWriter outputWriter = null;
		
		try {
			  fileOutputStream=new FileOutputStream(destFile);  
	          outputWriter=new OutputStreamWriter(fileOutputStream,"UTF-8");  
	          outputWriter.write(data);
	       
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(outputWriter);
			IOUtils.closeQuietly(fileOutputStream);
		}
       
		return true;
	}
	
	public static Map<String, Object> readLineFile(String filePath, String fileName) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();

		try {
			File destFile = new File(filePath, fileName);
			String jsonStr = null;
			if (destFile.exists()) {
				BufferedReader br = new BufferedReader(
						new InputStreamReader(new FileInputStream(filePath + fileName), "UTF-8"));
				jsonStr = br.readLine();
				jsonMap = JsonUtil.jsonToPojo(jsonStr, Map.class);

			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return jsonMap;
	}
	
	
	/**
	 * 新建目录
	 * 
	 * @param folderPath
	 *            String �? c:/fqf
	 * @return boolean
	 */
	public static int createFloder(String folderPath) {
		try {
			String filePath = folderPath;
			filePath = filePath.toString();
			File myFilePath = new File(filePath);
			if(myFilePath.isFile()){
				myFilePath.getParentFile().mkdirs();
				return 0;
			}else{
				if (!myFilePath.exists()) {
					myFilePath.mkdirs();
					return 0;
				} else {
					return 1;
				}
			}
		} catch (Exception e) {
			return 2;
		}
	}
	
	public static void appendInfoToFile(String fileName, String info) {
		File file = new File(fileName);
		FileWriter fileWriter = null;
		try {
			if(!file.exists()) {
				file.createNewFile();
			}
			fileWriter = new FileWriter(file, true);
			info =info +System.getProperty("line.separator");
			fileWriter.write(info);
			fileWriter.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
        	if(null != fileWriter) {
        		try {
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
        	}
		}
	}
	
	public static boolean deleteFile(String fileName) {
		boolean flag = true;
		try {
			File file = new File(fileName);
			if (file.exists()) {
				file.delete();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			flag =false;
		}
		return flag;
		
		
	}
	
	/**
	 * 得到文件后缀名
	 * @param fileName
	 * @return
	 */
	public static String getSuffixName(String fileName) {
		String suffixName = "";
		if (StringUtils.isNotBlank(fileName) && fileName.lastIndexOf(".") != -1) {
			suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);
		}
			
		return suffixName;
	}
	
	
	
	

	
	
	
}





